package bz.ziro.kanbe.controller.mng.page;

import java.util.Date;
import java.util.logging.Logger;
import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;
import org.slim3.datastore.Datastore;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

import bz.ziro.kanbe.dao.PageDao;
import bz.ziro.kanbe.model.Page;

/**
 * ページのXML部分の更新
 * @author Administrator
 */
public class DataUpdateController extends Controller {

    @SuppressWarnings("unused")
    private static final Logger logger = Logger.getLogger(DataUpdateController.class.getName());

    @Override
    public Navigation run() {

        String siteKeyBuf = sessionScope("siteKey");
        String keyBuf = requestScope("pageKey");
        //ページデータを取得
        Page page = PageDao.find(Long.valueOf(siteKeyBuf),Long.valueOf(keyBuf));

        String data = requestScope("data");
        String type = requestScope("type");

        if ( type.equals("site") ) {
            //XMLをデータとして設定
            page.setSiteData(data);
        } else {
            //XMLをデータとして設定
            page.setPageData(data);
        }

        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();
        //作成日を設定
        page.setEditDate(new Date());
        page.setEditor(user);
        Datastore.put(page);

        return null;
    }
}
